package com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.entity.TEnterpriseArchivesInfo;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.entity.TEnterpriseArchivesAnnualReport;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.service.ITEnterpriseArchivesAnnualReportService;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.entity.TEnterpriseArchivesProduct;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.service.ITEnterpriseArchivesProductService;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.dto.TEnterpriseArchivesInfoListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.mapper.TEnterpriseArchivesInfoMapper;
import com.xjrsoft.module.customerTwo.barin.t_enterprise_archives_info.service.ITEnterpriseArchivesInfoService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author dongxinhang
 * @since 2025-05-28
 */
@Service
@AllArgsConstructor
public class TEnterpriseArchivesInfoServiceImpl extends BaseService<TEnterpriseArchivesInfoMapper, TEnterpriseArchivesInfo> implements ITEnterpriseArchivesInfoService {

	private ITEnterpriseArchivesAnnualReportService tEnterpriseArchivesAnnualReportService;
	private ITEnterpriseArchivesProductService tEnterpriseArchivesProductService;

	@Override
	public IPage<TEnterpriseArchivesInfo> getPageList(TEnterpriseArchivesInfoListDto pageListDto) {
		Wrapper<TEnterpriseArchivesInfo> wrapper = Wrappers.<TEnterpriseArchivesInfo>query().lambda()
				.like(!StringUtil.isEmpty(pageListDto.getEnterprise_name()), TEnterpriseArchivesInfo::getEnterpriseName, pageListDto.getEnterprise_name())
				.like(!StringUtil.isEmpty(pageListDto.getProduction_mode()), TEnterpriseArchivesInfo::getProductionMode, pageListDto.getProduction_mode())
				.like(!StringUtil.isEmpty(pageListDto.getLeader_name()), TEnterpriseArchivesInfo::getLeaderName, pageListDto.getLeader_name())
				.ge(!StringUtil.isEmpty(pageListDto.getEstablishment_date_Start()), TEnterpriseArchivesInfo::getEstablishmentDate, pageListDto.getEstablishment_date_Start())
				.le(!StringUtil.isEmpty(pageListDto.getEstablishment_date_End()), TEnterpriseArchivesInfo::getEstablishmentDate, pageListDto.getEstablishment_date_End());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addTEnterpriseArchivesInfo(TEnterpriseArchivesInfo tEnterpriseArchivesInfo, List<TEnterpriseArchivesAnnualReport> tEnterpriseArchivesAnnualReportList, List<TEnterpriseArchivesProduct> tEnterpriseArchivesProductList) {
		String tEnterpriseArchivesInfoId = IdWorker.get32UUID();
		tEnterpriseArchivesInfo.setId(tEnterpriseArchivesInfoId);
		tEnterpriseArchivesAnnualReportList.forEach(tEnterpriseArchivesAnnualReport -> tEnterpriseArchivesAnnualReport.setEnterpriseArchivesId(tEnterpriseArchivesInfoId));
		tEnterpriseArchivesAnnualReportService.saveBatch(tEnterpriseArchivesAnnualReportList);
		tEnterpriseArchivesProductList.forEach(tEnterpriseArchivesProduct -> tEnterpriseArchivesProduct.setEnterpriseArchivesId(tEnterpriseArchivesInfoId));
		tEnterpriseArchivesProductService.saveBatch(tEnterpriseArchivesProductList);
		boolean isSuccess = this.save(tEnterpriseArchivesInfo);
		return isSuccess ? tEnterpriseArchivesInfoId : null;
	}

	@Override
	public boolean updateTEnterpriseArchivesInfo(String id, TEnterpriseArchivesInfo tEnterpriseArchivesInfo, List<TEnterpriseArchivesAnnualReport> tEnterpriseArchivesAnnualReportList, List<TEnterpriseArchivesProduct> tEnterpriseArchivesProductList) {
		tEnterpriseArchivesInfo.setId(id);
		// TEnterpriseArchivesAnnualReport
		List<String> tEnterpriseArchivesAnnualReportIdList = new ArrayList<>();
		for (TEnterpriseArchivesAnnualReport tEnterpriseArchivesAnnualReport : tEnterpriseArchivesAnnualReportList) {
			String tEnterpriseArchivesAnnualReportId = tEnterpriseArchivesAnnualReport.getId();
			if (StringUtil.isEmpty(tEnterpriseArchivesAnnualReportId)) {
				tEnterpriseArchivesAnnualReport.setEnterpriseArchivesId(id);
			} else {
				tEnterpriseArchivesAnnualReportIdList.add(tEnterpriseArchivesAnnualReportId);
			}
		}
		if (CollectionUtil.isNotEmpty(tEnterpriseArchivesAnnualReportIdList)) {
			tEnterpriseArchivesAnnualReportService.remove(Wrappers.<TEnterpriseArchivesAnnualReport>query().lambda()
					.notIn(TEnterpriseArchivesAnnualReport::getId, tEnterpriseArchivesAnnualReportIdList)
					.eq(TEnterpriseArchivesAnnualReport::getEnterpriseArchivesId, id));
		}
		tEnterpriseArchivesAnnualReportService.saveOrUpdateBatch(tEnterpriseArchivesAnnualReportList);
		// TEnterpriseArchivesProduct
		List<String> tEnterpriseArchivesProductIdList = new ArrayList<>();
		for (TEnterpriseArchivesProduct tEnterpriseArchivesProduct : tEnterpriseArchivesProductList) {
			String tEnterpriseArchivesProductId = tEnterpriseArchivesProduct.getId();
			if (StringUtil.isEmpty(tEnterpriseArchivesProductId)) {
				tEnterpriseArchivesProduct.setEnterpriseArchivesId(id);
			} else {
				tEnterpriseArchivesProductIdList.add(tEnterpriseArchivesProductId);
			}
		}
		if (CollectionUtil.isNotEmpty(tEnterpriseArchivesProductIdList)) {
			tEnterpriseArchivesProductService.remove(Wrappers.<TEnterpriseArchivesProduct>query().lambda()
					.notIn(TEnterpriseArchivesProduct::getId, tEnterpriseArchivesProductIdList)
					.eq(TEnterpriseArchivesProduct::getEnterpriseArchivesId, id));
		}
		tEnterpriseArchivesProductService.saveOrUpdateBatch(tEnterpriseArchivesProductList);
		return this.updateById(tEnterpriseArchivesInfo);
	}

	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			tEnterpriseArchivesAnnualReportService.remove(Wrappers.<TEnterpriseArchivesAnnualReport>query().lambda().in(TEnterpriseArchivesAnnualReport::getEnterpriseArchivesId, ids));
			tEnterpriseArchivesProductService.remove(Wrappers.<TEnterpriseArchivesProduct>query().lambda().in(TEnterpriseArchivesProduct::getEnterpriseArchivesId, ids));
			return true;
		}
		return false;
	}

	public List<TEnterpriseArchivesAnnualReport> getTEnterpriseArchivesAnnualReportByParentId(String parentId){
		Wrapper wrapper = Wrappers.<TEnterpriseArchivesAnnualReport>query().lambda().eq(TEnterpriseArchivesAnnualReport::getEnterpriseArchivesId, parentId);
		return tEnterpriseArchivesAnnualReportService.list(wrapper);
	}
	public List<TEnterpriseArchivesProduct> getTEnterpriseArchivesProductByParentId(String parentId){
		Wrapper wrapper = Wrappers.<TEnterpriseArchivesProduct>query().lambda().eq(TEnterpriseArchivesProduct::getEnterpriseArchivesId, parentId).orderByAsc(TEnterpriseArchivesProduct::getOrderNum);
		return tEnterpriseArchivesProductService.list(wrapper);
	}
}